package com.ssm.controller;

import com.ssm.entity.User;
import com.ssm.service.UserServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/uc")
public class UserController extends BaseController{

    @Resource(name = "userServiceI")
    private UserServiceI usi;


    @RequestMapping("/index")
    public String index(){
        return prefix + "/user/userIndex" + suffix;
    }

    @RequestMapping("/aoe")
    public String aoe(HttpServletRequest req){

        String id = req.getParameter("id");
        if(id!=null&&id!=""){
            int idi = Integer.parseInt(id);//把字符串id转换为整数id
            User u = new User();
            u.setId(idi);
            u = usi.get(u);
            if(u!=null){
                req.setAttribute("u",u);
            }
        }
        return prefix + "/user/aoe" + suffix;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(User user, HttpSession session){
        return usi.login(user,session);
    }

    @RequestMapping("/loginOut")
    @ResponseBody
    public String loginOut(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        return "success";
    }



    @RequestMapping("/find")
    @ResponseBody
    public List<User> find(User u){
        return usi.find(u);
    }


    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(User u){
        return usi.add(u);
    }


    @RequestMapping("/editUser")
    @ResponseBody
    public Map<String,Object> editUser(User u){
        return usi.edit(u);
    }

    @RequestMapping("/removeUser/{id}")
    @ResponseBody
    public Map<String,Object> removeUser(@PathVariable(value = "id") int uid){
        User u = new User();
        u.setId(uid);
        return usi.remove(u);
    }


}
